from right_UI import Right_UI
from PyQt5 import QtCore, QtGui, QtWidgets
import sys, base64, sip
import qtawesome, requests, re
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QCursor, QImage, QPixmap
from io import BytesIO
import ast

class child(Right_UI):
    def right_book_ui(self,book_num):
        self.clean_all()
        self.right_base_ui()
        self.title_num = 0
        self.right_book_widget = QtWidgets.QWidget()  # 推荐封面部件
        self.right_book_layout = QtWidgets.QGridLayout()  # 推荐封面网格布局
        self.right_book_widget.setLayout(self.right_book_layout)

        self.right_jieshao_widget = QtWidgets.QWidget()  # 推荐封面部件
        self.right_jieshao_layout = QtWidgets.QGridLayout()  # 推荐封面网格布局
        self.right_jieshao_widget.setLayout(self.right_jieshao_layout)

        self.right_white_widget = QtWidgets.QWidget()  # 推荐封面部件
        self.right_white_layout = QtWidgets.QGridLayout()  # 推荐封面网格布局
        self.right_white_widget.setLayout(self.right_white_layout)

        self.right_white_widget_2 = QtWidgets.QWidget()  # 推荐封面部件
        self.right_white_layout_2 = QtWidgets.QGridLayout()  # 推荐封面网格布局
        self.right_white_widget_2.setLayout(self.right_white_layout_2)

        self.book_button_1 = QtWidgets.QToolButton()
        self.book_button_1.setObjectName('book_img')
        self.book_button_1.setIcon(self.ico(self.iico[book_num]))  # 设置按钮图标
        self.book_button_1.setIconSize(QtCore.QSize(150, 200))  # 设置图标大小
        self.book_button_1.setFont(QtGui.QFont('SansSerif', 10))
        self.book_button_1.setToolTip(self.items[book_num][2])

        self.right_bar_widget_jieshao = QtWidgets.QTextEdit()
        self.right_bar_widget_jieshao.setObjectName('right_bar_widget_jieshao')
        self.book_value=self.jianjie[book_num].replace('                                                        ','    ')
        self.right_bar_widget_jieshao.setPlainText(self.book_value)
        self.right_bar_widget_jieshao.setReadOnly(True)

        self.book_label_1 = QtWidgets.QLabel('书名：%s'%self.items[book_num][2])
        self.book_label_1.setObjectName('book_label')
        self.book_label_2 = QtWidgets.QLabel('作者：%s'%self.items[book_num][3])
        self.book_label_2.setObjectName('book_label')
        self.book_label_3 = QtWidgets.QLabel('分类：')
        self.book_label_3.setObjectName('book_label')
        self.book_label_4 = QtWidgets.QLabel('成绩：')
        self.book_label_4.setObjectName('book_label')
        self.book_label_5 = QtWidgets.QLabel('字数：万字')
        self.book_label_5.setObjectName('book_label')

        self.read_button_1=QtWidgets.QPushButton(qtawesome.icon('fa.book', color='black'),"在线阅读")
        self.read_button_1.setObjectName('read_button')
        print(book_num)

        self.read_button_1.clicked.connect(lambda:self.right_book_read(book_num))
        self.read_button_2=QtWidgets.QPushButton(qtawesome.icon('fa.download', color='black'),"在线下载")
        self.read_button_2.setObjectName('read_button')

        self.read_Label_3=QtWidgets.QLabel('')
        self.read_Label_4=QtWidgets.QLabel('')



        self.right_layout.addWidget(self.right_book_widget, 1, 1, 5, 6)
        self.right_layout.addWidget(self.right_jieshao_widget, 6, 1, 8, 22)
        self.right_layout.addWidget(self.right_white_widget, 15, 0, 2, 20)
        self.right_layout.addWidget(self.right_white_widget_2, 0, 22, 19, 8)

        self.right_book_layout.addWidget(self.book_button_1,0,0, 5, 1,Qt.AlignLeft)
        self.right_book_layout.addWidget(self.book_label_1, 0,1, 1, 1,Qt.AlignLeft)
        self.right_book_layout.addWidget(self.book_label_2, 1,1, 1, 1,Qt.AlignLeft)
        self.right_book_layout.addWidget(self.book_label_3, 2,1, 1, 1,Qt.AlignLeft)
        self.right_book_layout.addWidget(self.book_label_4, 3,1, 1, 1,Qt.AlignLeft)
        self.right_book_layout.addWidget(self.book_label_5, 4,1, 1, 1,Qt.AlignLeft)

        self.right_white_layout.addWidget(self.read_Label_4, 0,0, 1, 2)
        self.right_white_layout.addWidget(self.read_button_1, 0,2, 1, 1)
        self.right_white_layout.addWidget(self.read_Label_3, 0,3, 1, 7)
        self.right_white_layout.addWidget(self.read_button_2, 0,10, 1, 1)
        self.right_jieshao_layout.addWidget(self.right_bar_widget_jieshao, 0,0, 5, 10)

        self.right_widget.setStyleSheet('''
                QToolButton#book_img{
                border:none;
                }
                ''')
        self.right_book_widget.setStyleSheet('''
                QLabel#book_label{
                font-size: 75 12pt \"Agency FB\";
                font-wight:600;
                }
                ''')
        self.right_jieshao_widget.setStyleSheet('''
                QTextEdit#right_bar_widget_jieshao{
                background:rgb(247, 247, 247);
                font-size:10pt;
                }
                ''')

    def right_book_ui_select(self, book_num):
            self.clean_all()
            self.right_base_ui()
            all=self.all
            book_num=int(book_num)
            print(book_num,all)
            self.right_book_widget = QtWidgets.QWidget()  # 推荐封面部件
            self.right_book_layout = QtWidgets.QGridLayout()  # 推荐封面网格布局
            self.right_book_widget.setLayout(self.right_book_layout)

            self.right_jieshao_widget = QtWidgets.QWidget()  # 推荐封面部件
            self.right_jieshao_layout = QtWidgets.QGridLayout()  # 推荐封面网格布局
            self.right_jieshao_widget.setLayout(self.right_jieshao_layout)

            self.right_white_widget = QtWidgets.QWidget()  # 推荐封面部件
            self.right_white_layout = QtWidgets.QGridLayout()  # 推荐封面网格布局
            self.right_white_widget.setLayout(self.right_white_layout)

            self.right_white_widget_2 = QtWidgets.QWidget()  # 推荐封面部件
            self.right_white_layout_2 = QtWidgets.QGridLayout()  # 推荐封面网格布局
            self.right_white_widget_2.setLayout(self.right_white_layout_2)

            self.book_button_1 = QtWidgets.QToolButton()
            self.book_button_1.setObjectName('book_img')
            #self.book_button_1.setIcon(self.ico(self.iico[book_num]))  # 设置按钮图标
            self.book_button_1.setIcon(self.ico(self.select_iico[int(book_num)]))  # 设置按钮图标
            self.book_button_1.setIconSize(QtCore.QSize(150, 200))  # 设置图标大小
            self.book_button_1.setFont(QtGui.QFont('SansSerif', 10))
            self.book_button_1.setToolTip(all[book_num][0])
            print(all)

            self.right_bar_widget_jieshao = QtWidgets.QTextEdit()
            self.right_bar_widget_jieshao.setObjectName('right_bar_widget_jieshao')
            self.book_value = all[book_num][6].replace('                                                        ','    ')
            self.right_bar_widget_jieshao.setPlainText(self.book_value)
            self.right_bar_widget_jieshao.setReadOnly(True)

            self.book_label_0 = QtWidgets.QLabel('书名:%s'%all[book_num][0])
            self.book_label_0.setObjectName('book_label')
            self.book_label_1 = QtWidgets.QLabel('作者:%s'%all[book_num][1])
            self.book_label_1.setObjectName('book_label')
            self.book_label_2 = QtWidgets.QLabel('分类:%s'%all[book_num][2])
            self.book_label_2.setObjectName('book_label')
            self.book_label_3 = QtWidgets.QLabel('状态:%s'%all[book_num][3])
            self.book_label_3.setObjectName('book_label')
            self.book_label_4 = QtWidgets.QLabel('字数:%s'%all[book_num][4])
            self.book_label_4.setObjectName('book_label')
            self.book_label_5 = QtWidgets.QLabel('推荐票:%s'%all[book_num][5])
            self.book_label_5.setObjectName('book_label')

            self.read_button_1 = QtWidgets.QPushButton(qtawesome.icon('fa.book', color='black'), "在线阅读")
            self.read_button_1.setObjectName('read_button')

            self.read_button_1.clicked.connect(lambda: self.right_book_read(book_num))
            self.read_button_2 = QtWidgets.QPushButton(qtawesome.icon('fa.download', color='black'), "在线下载")
            self.read_button_2.setObjectName('read_button')

            self.read_Label_3 = QtWidgets.QLabel('')
            self.read_Label_4 = QtWidgets.QLabel('')

            self.right_layout.addWidget(self.right_book_widget, 1, 1, 5, 6)
            self.right_layout.addWidget(self.right_jieshao_widget, 6, 1, 8, 22)
            self.right_layout.addWidget(self.right_white_widget, 15, 0, 2, 20)
            self.right_layout.addWidget(self.right_white_widget_2, 0, 22, 19, 8)

            self.right_book_layout.addWidget(self.book_button_1, 0, 0, 6, 1, Qt.AlignLeft)
            self.right_book_layout.addWidget(self.book_label_0, 0, 1, 1, 1, Qt.AlignLeft)
            self.right_book_layout.addWidget(self.book_label_1, 1, 1, 1, 1, Qt.AlignLeft)
            self.right_book_layout.addWidget(self.book_label_2, 2, 1, 1, 1, Qt.AlignLeft)
            self.right_book_layout.addWidget(self.book_label_3, 3, 1, 1, 1, Qt.AlignLeft)
            self.right_book_layout.addWidget(self.book_label_4, 4, 1, 1, 1, Qt.AlignLeft)
            self.right_book_layout.addWidget(self.book_label_5, 5, 1, 1, 1, Qt.AlignLeft)

            self.right_white_layout.addWidget(self.read_Label_4, 0, 0, 1, 2)
            self.right_white_layout.addWidget(self.read_button_1, 0, 2, 1, 1)
            self.right_white_layout.addWidget(self.read_Label_3, 0, 3, 1, 7)
            self.right_white_layout.addWidget(self.read_button_2, 0, 10, 1, 1)
            self.right_jieshao_layout.addWidget(self.right_bar_widget_jieshao, 0, 0, 5, 10)

            self.right_widget.setStyleSheet('''
                    QToolButton#book_img{
                    border:none;
                    }
                    ''')
            self.right_book_widget.setStyleSheet('''
                    QLabel#book_label{
                    font-size: 75 12pt \"Agency FB\";
                    font-wight:600;
                    }
                    ''')
            self.right_jieshao_widget.setStyleSheet('''
                    QTextEdit#right_bar_widget_jieshao{
                    background:rgb(247, 247, 247);
                    font-size:10pt;
                    }
                    ''')

    def right_book_read_value(self,book_num,i):
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.75 Safari/537.36',
        }
        if i == 'up':
            self.title_num=self.title_num+1
        elif i == 'down':
            if self.title_num >0:
                self.title_num = self.title_num - 1
        elif i == 's':
            self.title_num=self.title_num
        url = "https:%s" % self.items[book_num][1]
        html = requests.get(url, headers=headers)
        a_1 = re.findall(r'<ul class="cf">(.*?)</ul>', html.text, re.S)
        a_title = re.findall(r'<li data-rid="(.*?)"><a href="(.*?)" target="_blank" data-eid=".*?" data-cid=".*?" title=".*?">(.*?)</a>[\S\s.].*?</li>',a_1[0], re.S)
        url_1 = "https:%s" % a_title[self.title_num][1]
        html = requests.get(url_1, headers=headers)
        aaa = html.text
        a_value = re.findall(r'<div class="read-content j_readContent" id="">(.*?)</div>.*?<div class="admire-wrap">',aaa, re.S)
        self.right_widget_read_book.setPlainText(a_title[self.title_num][2]+'\n'+a_value[0].replace('\n', '').replace('<p>', '\n'))
        self.right_widget_read_book.setReadOnly(True)

    def right_book_read(self,book_num):
        self.right_base_ui()
        self.book_read_button_1=QtWidgets.QPushButton(qtawesome.icon('fa.arrow-left', color='black'),"上一章")
        self.book_read_button_1.setObjectName('read_button')
        self.book_read_button_2 = QtWidgets.QPushButton(qtawesome.icon('fa.list-ul', color='black'), "目录")
        self.book_read_button_2.setObjectName('read_button')
        self.book_read_button_3=QtWidgets.QPushButton(qtawesome.icon('fa.arrow-right', color='black'),"下一章")
        self.book_read_button_3.setObjectName('read_button')
        self.right_widget_read_book = QtWidgets.QTextEdit()
        self.right_widget_read_book.setObjectName('right_widget_read_book')
        if self.title_num == 0:
            self.right_book_read_value(book_num,i='s')

        self.right_layout.addWidget(self.right_book_read_widget, 0, 0, 30, 30)
        self.right_layout.addWidget(self.right_book_read_widget_button, 30, 0, 1, 30)
        self.right_book_read_layout.addWidget(self.right_widget_read_book, 0,0, 30, 29)
        self.right_book_read_layout_button.addWidget(self.book_read_button_1, 0,1, 1, 1)
        self.right_book_read_layout_button.addWidget(self.book_read_button_2, 0,2, 1, 1)
        self.right_book_read_layout_button.addWidget(self.book_read_button_3, 0,3, 1, 1)
        self.book_read_button_1.clicked.connect(lambda: self.right_book_read_value(book_num,i='down'))
        self.book_read_button_3.clicked.connect(lambda: self.right_book_read_value(book_num,i='up'))

        self.right_set_tools_UI()


def main():
    app = QtWidgets.QApplication(sys.argv)
    gui = child()
    gui.show()

    sys.exit(app.exec_())


if __name__ == '__main__':
    main()